/**
 * @Date: 2022-04-05 19:47:55
 * @LastEditors: sailornpg
 * @LastEditTime: 2022-04-21 13:29:20
 * @FilePath: \weibo-api\routes\users.js
 * @Description: 用户路由
 */
// @ts-nocheck
const router = require("koa-router")();
const {
  checkUserExist,
  checkLikeExist,
  followObjectIsMe,
} = require("../middleware/check");
const { auth } = require("../middleware/loginCheck");
const { SuccessModel, ErrorModel } = require("../ResModel/index");
const { userValidator } = require("../middleware/validator");
const {
  getMeCount,
  getAtMeBlogList,
  getAtMeReplyList,
  markAsRead,
  markBlogAsRead,
} = require("../controller/atCtl");
const {
  createUser,
  login,
  updateUserInfo,
  logout,
  getOtherUserInfo,
  getFansList,
  following,
  unfollowing,
  getFollowList,
} = require("../controller/userCtl");
const { likeBlog, unLikeBlog } = require("../controller/likeCtl");
router.prefix("/api/users");
//注册
router.post("/register", userValidator, checkUserExist, createUser);
//登陆
router.post("/login", checkUserExist, login);
//获取@我的数量
router.get("/atMe", auth, getMeCount);
//获取@我的博客列表
router.get("/atMeBlogList", auth, getAtMeBlogList);
//获取@我的评论列表
router.get("/atMeReplyList", auth, getAtMeReplyList);
//获取登陆用户信息
router.get("/", async (ctx, next) => {
  // console.log(ctx.session);
  if (ctx.session.userInfo) {
    ctx.body = new SuccessModel(ctx.session.userInfo);
  } else {
    ctx.body = new ErrorModel(204, "没有用户信息");
  }
});
//已读提到我的回复
router.post("/read/:blogId/:ReplyId/:commentId", auth, markAsRead);
//已读提到我的博客@
router.put("/readblog/:blogId", auth, markBlogAsRead);
//修改用户信息
router.patch("/:id", auth, updateUserInfo);
//退出登陆
router.post("/logout", auth, logout);
//获取其他用户信息
router.get("/:id", getOtherUserInfo);
//获取粉丝列表
router.get("/follower/:id", getFansList);
//获取关注列表
router.get("/following/:id", getFollowList);
//关注某人
router.put("/follow/:id", auth, followObjectIsMe, following);
//取消关注某人
router.delete("/follow/:id", auth, unfollowing);
//点赞博客
router.put("/like/:blogId", auth, checkLikeExist, likeBlog);
//取消点赞
router.delete("/unLike/:blogId", auth, unLikeBlog);
module.exports = router;
